/**
 * 功能:红包系统
 * From:芬芬时尚潮流
 */

var status = -1;
var iconQ = "#fUI/UIWindow2/QuestAlarm/BtQ/normal/0#";
var 空心圆圈1 = "#fUI/UIWindow2/bohabManager/dot/1/dot#";
var 空心圆圈2 = "#fUI/UIWindow2/bohabManager/dot/2/dot#";
var 空心圆圈3 = "#fUI/UIWindow2/bohabManager/dot/3/dot#";
var 空心圆圈4 = "#fUI/UIWindow2/bohabManager/dot/4/dot#";
var 任务简介 = "#fUI/UIWindow2.img/Quest/quest_info/summary_icon/summary#"
	var Gift = "#fUI/UIWindow2/crossHunterUI/reward/button/normal/0#";

var 音符 = "#fEffect/CharacterEff/1032063/0/0#";
var UI0 = "#fEffect/CharacterEff/1051296/1/0#";
var UI1 = "#fEffect/CharacterEff/1051296/1/1#";
var UI2 = "#fEffect/CharacterEff/1051296/1/2#";
var UI3 = "#fEffect/CharacterEff/1051296/1/3#";
var UI4 = "#fEffect/CharacterEff/1051296/1/4#";
var UI5 = "#fEffect/CharacterEff/1051296/1/5#";
var 音符横条 = 音符 + 音符 + 音符 + 音符 + 音符 + 音符 + 音符;
var 蓝星横条 = UI0 + UI1 + UI2 + UI3 + UI4 + UI5 + UI0 + UI1 + UI2 + UI3 + UI4 + UI5 + UI0 + UI1 + UI2 + UI3 + UI4;

var a = "任性红包";
var LuckyMoney = 0;
var LuckMoneyType = 0;
var monArr = new Array();

function action(mode, type, selection) {
	if (status >= 0 && mode == 0) {
		cm.dispose();
		return;
	}
	if (mode == 1) {
		status++;
	} else {
		status--;
	}
	if (status == 0) {
		var text = "";
		text += "" + 音符横条 + "\r\n";
		text += "                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n";
		text += "#b亲爱的#r" + cm.getPlayer().getName() + "#k,#b欢迎来到#r" + cm.getServerName() + "#k!#b微信的红包系统带来的乐趣同样在" + cm.getServerName() + "也可以体验的到哦，只要有土豪心情好.一言不合就砸红包.炫富必备哇....\r\n\r\n";
		text += "#L0# " + 空心圆圈1 + " #d[不劳而获]#r我要抢红包~\r\n\r\n"
		text += "#L1# " + 空心圆圈2 + " #d[有钱任性]#r我要发红包~\r\n\r\n"
		text += "" + 蓝星横条 + "\r\n";
		cm.sendSimple(text);
	} else if (status == 1) {
		if (selection == 0) { //领取管理员发布的红包
			status = 5;
			var text = findLuckeyMoney(0);
			if (text == "") {
				cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n#r目前红包已经被抢完，请期待下个土豪来个大红包吧~");
				cm.dispose();
				return;
			}
			cm.sendSimple(text);
		} else if (selection == 1) { //管理红包设置
			cm.sendSimple("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n#b#L0# 请选择您要发送的红包类型\r\n\r\n" + (cm.getPlayer().isGM() ? "#L1# 管理目前的红包。[GM]" : ""));
		}
	} else if (status == 2) {
		if (selection == 0) { //生成一个红包
			var tx = "";
			tx += "" + 音符横条 + "\r\n";
			tx += "                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n";
			tx += "" + 蓝星横条 + "\r\n\r\n";
			tx += "   #b#L0#" + 空心圆圈1 + " 金币#k#l";
			tx += "		#d#L1#" + 空心圆圈2 + " 点券#k#l";
			tx += "		#g#L2#" + 空心圆圈3 + " 抵用#k#l\r\n\r\n";
			tx += "		  #b#L5#" + 空心圆圈2 + " 微积分#k#l";
			tx += "		#d#L6#" + 空心圆圈3 + " 活跃值#k#l\r\n\r\n";
			tx += "" + (cm.getPlayer().isGM() ? "#L4#" + 空心圆圈2 + " 道具[GM]" : "");
			cm.sendSimple(tx);

		} else if (selection == 1) { //管理目前的红包[GM]
			status = 6;
			var text = findLuckeyMoney(1);
			if (text == "") {
				cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n#r暂时没有红包数据。#k#l");
				cm.dispose();
				return;
			}
			cm.sendSimple(findLuckeyMoney(1));
		}
	} else if (status == 3) {
		switch (selection) {
		case 0: //金币
			cm.sendGetNumber("请输入您想要输入的金币数量", 0, 0, 2100000000);
			break;
		case 1: //点券
			cm.sendGetNumber("请输入您想要输入的点券数量", 0, 0, 10000);
			break;
		case 2: //抵用
			cm.sendGetNumber("请输入您想要输入的抵用数量", 0, 0, 10000000);
			break;
		case 3: //现金
			cm.sendGetNumber("请输入您想要输入的现金数量", 0, 0, 100);
			break;
		case 4: //道具
			cm.sendGetNumber("请输入道具ID", 0, 0, 9999999);
			break;
		case 5: //积分
			cm.sendGetNumber("请输入您想要输入的积分值数量", 0, 0, 10000000);
			break;
		case 6: //活跃
			cm.sendGetNumber("请输入您想要输入的活跃值数量", 0, 0, 10000000);
			break;
		}
		LuckMoneyType = selection; //记录红包内礼物的类别
	} else if (status == 4) {
		LuckyMoney = selection;
		cm.sendGetNumber("你刚才设置的1个红包的的金额将x你现在输入的数量.（1人只能领取一次）", 0, 1, 50);
	} else if (status == 5) {
		if (!cm.getPlayer().isGM()) { //不是GM的话 就要检测是否满足发送条件
			switch (LuckMoneyType) {
			case 0: //金币
				a = "冒险币";
				if (cm.getMeso() >= LuckyMoney * selection) {
					cm.gainMeso(-LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + "." + a);
					cm.dispose();
					return;
				}
				break;
			case 1: //点券
				a = "点券";
				if (cm.getNX(1) >= LuckyMoney * selection) {
					cm.gainNX(1, -LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + ".点" + a);
					cm.dispose();
					return;
				}
				break;
			case 2: //抵用
				a = "抵押劵";
				if (cm.getNX(2) >= LuckyMoney * selection) {
					cm.gainNX(2, -LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + ".点" + a);
					cm.dispose();
					return;
				}
				break;
			case 3: //现金
				a = "余额";
				if (cm.HyPay(1) >= LuckyMoney * selection) {
					cm.addHyPay(LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + ".点" + a);
					cm.dispose();
					return;
				}
				break;
			case 5: //积分
				a = "积分值";
				if (cm.getPlayerEnergy() >= LuckyMoney * selection) {
					cm.gainPlayerEnergy(-LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + ".点" + a);
					cm.dispose();
					return;
				}
				break;
			case 6: //活跃
				a = "活跃值";
				if (cm.getPlayerPoints() >= LuckyMoney * selection) {
					cm.gainPlayerPoints(-LuckyMoney * selection);
				} else {
					cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n发送红包失败\r\n你身上没有" + LuckyMoney * selection + ".点" + a);
					cm.dispose();
					return;
				}
				break;
			}
		}

		//红包类别	输入的红包数量  输入的最大领取人数
		//java.lang.System.out.println("发送的类型:"+LuckMoneyType+"		输入的金额:" + LuckyMoney + "		可领取的人数数量:" + selection);
		setLuckeyMoney(LuckMoneyType, LuckyMoney, selection); //更新数据库数据
		cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n#b红包生成成功!!\r\n\r\n清单如下:\r\n\r\n你1个红包的金额是:<#r" + LuckyMoney + "#b>点" + a + "\r\n\r\n你总共发了:<#r" + selection + "#b>个红包\r\n\r\n一共消耗了您:<#r" + LuckyMoney * selection + "#b>点" + a + "#l");
		for (var i = 0; i < 10; i++) {
			cm.worldSpouseMessage(0x15, "『红包系统』" + " : " + "哇塞，" + (cm.getPlayer().isGM() ? "" + cm.getServerName() + "的管理员" : "土豪< " + cm.getChar().getName() + " >") + "发了一个红包.赶紧抢");
		}
		cm.dispose();
		status = -1;
	} else if (status == 6) {
		getLuckeyMoney(selection);
		cm.dispose();
	} else if (status == 7) {
		delMoney(monArr[selection]);
		cm.sendNext("删除数据成功！");
		status = -1;
	}
}
function getLuckeyMoney(which) {
	if (cm.getBossLog("红包" + monArr[which]) >= 1) {
		cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n对不起，一个红包每个角色只能领取一次。");
		cm.dispose();
		return;
	}

	var conn = cm.getConnection();
	var sql = "select * from luckMoneyData where id =" + monArr[which] + " and  used < AllowPeople";
	var ps = conn.prepareStatement(sql);
	var getMoney = ps.executeQuery();

	//TODO 再次判断是否已经领取
	//var ii = cm.getItemInfo();
	//cm.getPlayer().dropMessage(-1, monArr[which]);
	while (getMoney.next()) {
		var name = String(getMoney.getString("name"));
		var type = parseInt(getMoney.getString("luckmoneytype"));
		var num = parseInt(getMoney.getString("LuckyMoney"));
		switch (type) {
		case 0: //金币
			cm.gainMeso(num);
			break;
		case 1: //点券
			cm.gainNX(1, num);
			break;
		case 2: //抵用
			cm.gainNX(2, num);
			break;
		case 3: //现金
			cm.addHyPay(-num);
			break;
		case 4: //道具
			cm.gainItem(num, 1);
			break;
		case 5: //积分
			cm.gainPlayerEnergy(num);
			break;
		case 6: //活跃
			cm.gainPlayerPoints(num);
			break;
		}

		cm.sendOk("" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n恭喜你抢到了来自土豪<" + name + "> 发的装有" + a + "<" + num + "> 的红包。");
		cm.worldSpouseMessage(0x15, "『红包系统』" + " : " + "玩家 " + cm.getChar().getName() + " 抢到了" + name + "发的装有" + a + "<" + num + "> 的红包。");
		iUsed(monArr[which], parseInt(getMoney.getString("used")) + 1);
		cm.setBossLog("红包" + monArr[which]);
		/*不知道谁写的.这里是while循环 拜托-0- 这样写必定报错
		getMoney.close();
		ps.close();
		conn.close();*/
	}
	getMoney.close();
	ps.close();
	conn.close();
}

function iUsed(uid, times) {
	var conn = cm.getConnection();
	var usedpp = conn.prepareStatement("update LuckMoneyData set used = ? where id =" + uid);
	usedpp.setString(1, times);
	usedpp.executeUpdate();
	usedpp.close();
	conn.close();
}

function findLuckeyMoney(index) {
	var conn = cm.getConnection();
	var getMoney;
	if (index == 0) { //0显示可用， 1显示所有
		getMoney = conn.prepareStatement("SELECT * FROM LuckMoneyData where used < AllowPeople").executeQuery();
	} else if (index == 1) {
		getMoney = conn.prepareStatement("SELECT * FROM LuckMoneyData").executeQuery();
	}
	var text = "";
	var i = 0;
	while (getMoney.next()) {
		text += "" + 音符横条 + "\r\n                #r#e " + cm.getServerName() + "红包系统 #d#n#k\r\n\r\n" + 蓝星横条 + "\r\n\r\n#L" + i + "#" + 空心圆圈2 + "#r<" + String(getMoney.getString("name")) + ">#b发送的第" + (i + 1) + "个神秘红包!#l\r\n";
		monArr.push(getMoney.getString("id"));
		i++;
	}
	getMoney.close();
	conn.close();
	return text;
}

function setLuckeyMoney(monType, monNumber, monMax) {
	var conn = cm.getConnection();
	var inserLuckeyMoney = conn.prepareStatement("INSERT INTO LuckMoneyData(id, luckmoneytype, LuckyMoney, AllowPeople, used,name) value(?,?,?,?,?,?)");
	inserLuckeyMoney.setString(1, null);
	inserLuckeyMoney.setString(2, monType);
	inserLuckeyMoney.setString(3, monNumber);
	inserLuckeyMoney.setString(4, monMax);
	inserLuckeyMoney.setString(5, 0);
	inserLuckeyMoney.setString(6, cm.getChar().getName());
	inserLuckeyMoney.executeUpdate();
	inserLuckeyMoney.close();
	conn.close();
}

function delMoney(uid) {
	var conn = cm.getConnection();
	var delectData = conn.prepareStatement("delete from LuckMoneyData where id = " + uid + "");
	delectData.executeUpdate(); //删除数据
	delectData.close();
	conn.close();
}
